package com.example.peibei.ui.weight;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.peibei.R;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

public class MoneyKeyboardDialog extends AlertDialog.Builder {

    public MoneyKeyboardDialog(Context context) {
        super(context, R.style.Theme_dialog);
    }

    public AlertDialog create(OnMoneyListener onMoneyListener) {
        AlertDialog dialog = super.create();
        dialog.show();
        Window window = dialog.getWindow();
//        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(attributes);
        window.setGravity(Gravity.BOTTOM);
        dialog.setContentView(R.layout.dialog_set_money);

        TextView tv_number = dialog.findViewById(R.id.tv_number);
        TextView x_tv = dialog.findViewById(R.id.x_tv);
        TextView ok_tv = dialog.findViewById(R.id.ok_tv);
        TextView cancel_tv = dialog.findViewById(R.id.cancel_tv);
        TextView tv_point = dialog.findViewById(R.id.tv_point);
        TextView tv_0 = dialog.findViewById(R.id.tv_0);
        TextView tv_1 = dialog.findViewById(R.id.tv_1);
        TextView tv_2 = dialog.findViewById(R.id.tv_2);
        TextView tv_3 = dialog.findViewById(R.id.tv_3);
        TextView tv_4 = dialog.findViewById(R.id.tv_4);
        TextView tv_5 = dialog.findViewById(R.id.tv_5);
        TextView tv_6 = dialog.findViewById(R.id.tv_6);
        TextView tv_7 = dialog.findViewById(R.id.tv_7);
        TextView tv_8 = dialog.findViewById(R.id.tv_8);
        TextView tv_9 = dialog.findViewById(R.id.tv_9);
        List<String> list = new ArrayList<>();
        View.OnClickListener inputListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView tv = (TextView) view;
                String s = tv.getText().toString();
                int point = list.indexOf(".");
                if (s.equals(".")){
                    if (point > 0 || list.isEmpty()){
                        return;
                    }
                }
                if (s.equals("0")){
                    if (list.size() == 1 && list.contains("0")){
                        return;
                    }
                }
                if (point > 0){
                    if (list.size()-point > 2){
                        return;
                    }
                }
                list.add(s);
                StringBuffer sb = new StringBuffer();
                for (String s1 : list) {
                    sb.append(s1);
                }
                tv_number.setText(sb.toString());
            }
        };
        x_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.size() > 0){
                    list.remove(list.size()-1);
                }
                StringBuffer sb = new StringBuffer();
                for (String s1 : list) {
                    sb.append(s1);
                }
                tv_number.setText(sb.toString());
            }
        });
        ok_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                if (list.size() > 0){
                    StringBuffer sb = new StringBuffer();
                    for (String s1 : list) {
                        sb.append(s1);
                    }
                    String number = sb.toString();
                    if (number.endsWith(".")){
                        number = sb.substring(0,sb.length()-1);
                    }
                    NumberFormat format = new DecimalFormat("#0.00");
                    onMoneyListener.onMoney(format.format(Double.parseDouble(number)));
                }
            }
        });
        cancel_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tv_point.setOnClickListener(inputListener);
        tv_0.setOnClickListener(inputListener);
        tv_1.setOnClickListener(inputListener);
        tv_2.setOnClickListener(inputListener);
        tv_3.setOnClickListener(inputListener);
        tv_4.setOnClickListener(inputListener);
        tv_5.setOnClickListener(inputListener);
        tv_6.setOnClickListener(inputListener);
        tv_7.setOnClickListener(inputListener);
        tv_8.setOnClickListener(inputListener);
        tv_9.setOnClickListener(inputListener);

        return dialog;
    }

    public interface OnMoneyListener{

        void onMoney(String money);
    }
}
